-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Week4 [STEP 1] cherrishRed #4
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cherrishRed
안녕하세요, 레드님.
클로저가 많이 어렵죠? ㅎㅎ 하지만 swift 및 iOS 개발에서 정말 자주 사용되는 개념이니, 지금 마구마구 부딫혀보는 것이 제일 좋습니다!
고민하셨던 점에 대해서 사견을 남겨보자면,
Exercise를 구조체로 설정하였는데요, 클래스로 변경하려고 하니(아래의 오류의 원인인가 싶어) init 을 설정할 때 (init 코드는 아래처럼 작성하였습니다.)
init(name: String, action: () -> Void) {
self.name = name
self.action = action
}
Assigning non-escaping parameter 'action' to an @escaping closure 라는 오류가 납니다.
제가 검색을 해 봤는데,escaping closure
라는 부분이 정확히 이해되지 않아, 우선은 구조체로 놔두었습니다.
아직 탈출 클로저(escaping closure)부분은 많이 어려울 수 있습니다. 이 부분은 조금 나중으로 미뤄두셔도 될 것 같습니다. 그리고 struct
를 class
로 변경해서 일어난 부분이 아니라, struct
의 멤버와이즈 이니셜라이저가 생성될 때, action
매개변수의 타입을 탈출 클로저로 생성해주어 그렇습니다. struct
도 class
와 같이 이니셜라이저를 직접 생성하게 되면, 똑같은 에러를 보실 수 있습니다. 🙂
Thread 1: Fatal error: Index out of range
Routine 클래스 안에 doRoutine 이라는 메소드를 만들었는데, 작동은 하는데 thread 가 납니다. 제가 생각기엔 다른 프로퍼티나 메소드를 건드리는 메소드도 아닌데, 도데체 어디서 오류를 뱉는지 모르겠습니다. 😂
게다가 fatal 오류 때문인지 그 위에
person.printBodyCondition()
코드도 실행하지 않네요. (저 코드만 단독으로 실행해 보았을 땐 잘 됩니다.)
그래서 1번에서 하려고 했던 Exercise 를 class 로 변경해 볼까 싶었지만... 그것도 아니었네요.
차근차근 살펴보면 무슨 문제인지 알 수 있습니다. 해당 부분에 힌트를 남겨놨으니, 참고하시면 해결하실 수 있을거에요. 🙂 화이팅!
CodeStarterCamp_Week4/main.swift
Outdated
init() { | ||
upperBodyStrength = 0 | ||
lowerBodyStrength = 0 | ||
muscularEndurance = 0 | ||
fatigue = 0 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
프로퍼티에 기본값을 주셨는데, 같은 동작을 하는 이니셜라이저가 필요할까요? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
앗 제가 클래스에는 이니셜라이저가 필요하다! 에 각인이 되어서 일단 이니셜라이저를 만들어 보았어요ㅠㅠ 없어도 될 것 같네요.
} | ||
} | ||
|
||
var person = BodyCondition() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
왜 변수로 인스턴스를 생성하셨나요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
운동하는 메서드를 사용하면서 person 안에 있는 프로퍼티 들이 변경되기 때문에 변수를 사용했습니다.
|
||
let activeRest = Exercise(name: "동적휴식", action: { | ||
let randomForFatigue = Int.random(in: 5..<10) | ||
person.fatigue += randomForFatigue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
휴식인데, 피로도가 증가하고 있습니다! 😱
|
||
class Routine { | ||
let name: String | ||
var exercises: [Exercise] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
딱히 exercises
에 변경이 일어날만한 코드가 보이지 않는데, 혹시 변수를 채택한 이유가 있나요? 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
혹시나 루틴을 변경할 수 도 있지 않나, 싶어서 일단은 변수로 생성 했습니다.
CodeStarterCamp_Week4/main.swift
Outdated
for count in 0...exercises.count { | ||
exercises[count].action() | ||
print("\(exercises[count].name)") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 부분에서 Index out of range 에러가 날 겁니다. 배열은 어디서부터 시작하는지 찬찬히 생각해봅시다! 😊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
제가 이부분을 정말 오해해서 몇일동안 코드를 건드려보고 그랬는데...(82번째 줄에 문제가 있는줄 알고)
결국엔 81번 째 줄에 범위에 관한 문제가 있다는 것을 아주아주 뒤늦게 깨닳았습니다...
@Journey36 |
@cherrishRed |
@Journey36
안녕하세요 글렌님.
step1 PR 합니다. 제가 오류를 해결하고 PR 을 하려고 했으나... 너무 오래 걸렸네요. (오류도 그대로고요😭)
고민되었던 점
이번 주차에서 고민을 많이 했다기 보다, 클로저를 사용하는 것이 너무 익숙하지 않아 애를 많이 먹었던 것 같습니다.
Exercise를 구조체로 설정하였는데요, 클래스로 변경하려고 하니(아래의 오류의 원인인가 싶어)
init 을 설정할 때 (init 코드는 아래처럼 작성하였습니다.)
Routine 클래스 안에 doRoutine 이라는 메소드를 만들었는데, 작동은 하는데 thread 가 납니다.
제가 생각기엔 다른 프로퍼티나 메소드를 건드리는 메소드도 아닌데, 도데체 어디서 오류를 뱉는지 모르겠습니다😂
게다가 fatal 오류 때문인지 그 위에
person.printBodyCondition()
코드도 실행하지 않네요. (저 코드만 단독으로 실행해 보았을 땐 잘 됩니다.)
그래서 1번에서 하려고 했던 Exercise 를 class 로 변경해 볼까 싶었지만... 그것도 아니었네요.
해결이 되지 않은 점, 조언을 얻고 싶은 부분
위에 두 문제를 해결하고 싶습니다.
fatal error 는 왜 뜨는가 를 알고 싶습니다.